﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FormSim
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=WIN8DEV;Initial Catalog=HOPP.MDF;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
            conn.Open();
            //string sqlquery = "SELECT * FROM Diver";
            SqlCommand command = new SqlCommand("GetDiver", conn);
            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader sdr = command.ExecuteReader();
            String str = "";
            while (sdr.Read())
            {
                str = str + sdr["FirstName"].ToString() + " ";
                str = str + sdr["LastName"].ToString();
                this.lbDiverList.Items.Add(str);
                str = "";
            }
            
            conn.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=WIN8DEV;Initial Catalog=HOPP.MDF;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
            conn.Open();
            SqlCommand command = new SqlCommand("InsertDiver", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FirstName", tbFirstName.Text));
            command.Parameters.Add(new SqlParameter("@LastName", tbLastName.Text));
            //SqlDataReader sdr = command.ExecuteReader();
            command.ExecuteNonQuery();
            conn.Close();
            lbDiverList.Items.Add(tbFirstName.Text+ " "+ tbLastName.Text);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=WIN8DEV;Initial Catalog=HOPP.MDF;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
            conn.Open();
            SqlCommand command = new SqlCommand("DeleteDiver", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@FirstName", tbFirstName.Text));
            command.Parameters.Add(new SqlParameter("@LastName", tbLastName.Text));
            //SqlDataReader sdr = command.ExecuteReader();
            command.ExecuteNonQuery();
            conn.Close();
            String remove = tbFirstName.Text + " " + tbLastName.Text;
            for (int n = lbDiverList.Items.Count - 1; n >= 0; --n)
            {
                if (lbDiverList.Items[n].ToString().Contains(remove))
                    lbDiverList.Items.RemoveAt(n);
            }
        }

        private void lbDiverList_Click(object sender, EventArgs e)
        {
            String str;
            str = lbDiverList.SelectedItem.ToString();
            String[] names = str.Split(' ');
            tbFirstName.Text = names[0];
            tbLastName.Text = names[1];
        }
    }
}
